package com.theyeasy.dvmini.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.theyeasy.dvmini.vo.Vo_msg;

public class TulingUtil {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    private final static String TULING_URL = "http://www.tuling123.com/openapi/api";
    private final static String APIKEY = "b26fce798262494b8f369f55a84df02d";

    public static Vo_msg getAnswer(String ask, String userid) {
        try {
            
            JsonObject o = new JsonObject();
            o.addProperty("key", APIKEY);
            o.addProperty("info", ask);
            o.addProperty("userid", userid);
            String result = CommonUtil.sendPost(TULING_URL, o.toString());
            Gson gson = new Gson();
            if (Strings.isNullOrEmpty(result)) return new Vo_msg(-1, "",result);
            TulingResult tulingResult = gson.fromJson(result, TulingResult.class);
            
            //正常返回答复内容
            if(tulingResult.getCode().equals("100000") && tulingResult.getText()!="") return new Vo_msg(0,"",tulingResult.getText());
            
            return new Vo_msg(-2, "", result);
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        
        return new Vo_msg(-3, "", "");        
    }

    class TulingResult {

        private String code;
        private String text;// 文本内容
        private String url;// url链接
        private Object list;// 新闻类返回列表

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public Object getList() {
            return list;
        }

        public void setList(Object list) {
            this.list = list;    
        }

    }
    
    public static void main(String[] args){
    	getAnswer("你好", "1");
    	 
    }

}
 